package xuegao.study.mall.web.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xuegao.study.mall.web.domain.CartPromotionItem;
import xuegao.study.mall.web.service.UmsMemberCouponService;
import xuegao.study.mall.web.service.UmsMemberService;
import xuegao.study.mall.repo.dao.sms.SmsCouponHistoryDao;
import xuegao.study.mall.common.domain.portal.SmsCouponHistoryDetail;
import xuegao.study.mall.common.model.sms.SmsCouponProductCategoryRelation;
import xuegao.study.mall.common.model.sms.SmsCouponProductRelation;
import xuegao.study.mall.common.model.ums.UmsMember;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UmsMemberCouponServiceImpl implements UmsMemberCouponService {

    @Autowired
    private UmsMemberService umsMemberService;

    @Autowired
    private SmsCouponHistoryDao smsCouponHistoryDao;

    @Override
    public List<SmsCouponHistoryDetail> listCart(List<CartPromotionItem> cartItemList, Integer type) {
        UmsMember currentMember = umsMemberService.getCurrentMember();
        Long memberId = currentMember.getId();
        Date now = new Date();
        //获取该用户所有优惠卷
        List<SmsCouponHistoryDetail> allList = smsCouponHistoryDao.getDetailList(memberId);
        //根据优惠卷使用类型判断优惠卷是否可用
        List<SmsCouponHistoryDetail> enableList = new ArrayList<>();
        List<SmsCouponHistoryDetail> disableList = new ArrayList<>();
        for(SmsCouponHistoryDetail couponHistoryDetail : allList) {
            Integer useType = couponHistoryDetail.getCoupon().getUseType();
            BigDecimal minPoint = couponHistoryDetail.getCoupon().getMinPoint();
            Date endTime = couponHistoryDetail.getCoupon().getEndTime();
            if (useType.equals(0)) {// 全场通用
                // 判断是否满足优惠起点
                // 计算购物车商品的总价
                BigDecimal totalAmount = calcTotalAmount(cartItemList);
                if (now.before(endTime) && totalAmount.subtract(minPoint).intValue() >= 0) {
                    enableList.add(couponHistoryDetail);
                } else {
                    disableList.add(couponHistoryDetail);
                }
            } else if (useType.equals(1)) {// 指定分类
                // 计算指定分类商品的总价
                List<Long> productCategoryIds = couponHistoryDetail.getCategoryRelationList()
                        .stream()
                        .map(SmsCouponProductCategoryRelation::getProductCategoryId)
                        .collect(Collectors.toList());
                BigDecimal totalAmount = calcTotalAmountByProductCategoryId(cartItemList, productCategoryIds);
                if (now.before(endTime) && totalAmount.intValue() > 0 && totalAmount.subtract(minPoint).intValue()>=0) {
                    enableList.add(couponHistoryDetail);
                } else {
                    disableList.add(couponHistoryDetail);
                }
            } else if (useType.equals(2)) {// 指定商品
                // 计算指定商品的总价
                List<Long> productIds = couponHistoryDetail.getProductRelationList()
                        .stream()
                        .map(SmsCouponProductRelation::getProductId)
                        .collect(Collectors.toList());
                BigDecimal totalAmount = calcTotalAmountByProductId(cartItemList, productIds);
                if (now.before(endTime) && totalAmount.intValue() > 0 && totalAmount.subtract(minPoint).intValue()>=0) {
                    enableList.add(couponHistoryDetail);
                } else {
                    disableList.add(couponHistoryDetail);
                }
            }
        }
        if (type.equals(1)) {
            return enableList;
        } else {
            return disableList;
        }
    }

    /**
     * 计算购物车中商品的总价
     * @param cartItemList cartItemList
     * @return 总价
     */
    private BigDecimal calcTotalAmount(List<CartPromotionItem> cartItemList) {
        BigDecimal total = new BigDecimal("0");
        for (CartPromotionItem item : cartItemList) {
             BigDecimal realPrice = item.getPrice().subtract(item.getReduceAmount());
             total = total.add(realPrice.multiply(new BigDecimal(item.getQuantity())));
        }
        return total;
    }

    /**
     * 计算购物车中指定商品分类的总价
     * @param cartItemList cartItemList
     * @param productCategoryIds 指定商品分类id
     * @return 总价
     */
    private BigDecimal calcTotalAmountByProductCategoryId(List<CartPromotionItem> cartItemList, List<Long> productCategoryIds) {
        BigDecimal total = new BigDecimal("0");
        for(CartPromotionItem item : cartItemList) {
            if (productCategoryIds.contains(item.getProductCategoryId())) {
                BigDecimal realPrice = item.getPrice().subtract(item.getReduceAmount());
                total = total.add(realPrice.multiply(new BigDecimal(item.getQuantity())));
            }
        }
        return total;
    }

    /**
     * 计算购物车中指定商品的总价
     * @param cartItemList cartItemList
     * @param productIds 指定商品id
     * @return 总价
     */
    private BigDecimal calcTotalAmountByProductId(List<CartPromotionItem> cartItemList, List<Long> productIds) {
        BigDecimal total = new BigDecimal("0");
        for(CartPromotionItem item : cartItemList) {
            if (productIds.contains(item.getProductId())) {
                BigDecimal realPrice = item.getPrice().subtract(item.getReduceAmount());
                total = total.add(realPrice.multiply(new BigDecimal(item.getQuantity())));
            }
        }
        return total;
    }
}
